import { EventManager } from "../event/EventManager";
import { HeroConfig, em_Effect_List } from "../settings/const";
import { AttrChangedType } from "./AttrChangedType";
import { BattleBuffEnum } from "./BattleBuffEnum";
import { NotifyEnum } from "./NotifyEnum";
import { OperationType } from "./OperationType";
import {SkillArray } from "./Skill";
import { SkillStateEnum } from "./SkillStateEnum";

/**
 * 代表单一输出指令的对象
 */
export class OperationItem {
    type: OperationType
    params: any
    EventIndex: number
    PreCondition: number

    constructor(item) {
        this.type = item.type;
        this.params = item.params;
        this.EventIndex = item.EventIndex;
        this.PreCondition = item.PreCondition;
    }

    

    /**
     * 翻译为可读格式
     */
    translate(names) {
        switch (this.type) {
            case OperationType.Start:
                this.params.desc = `${names[this.params.me]}和${names[this.params.enemy]}的对决开始了！`;
                break;

            case OperationType.Enter:
                this.params.desc = `${this.src(names)}进场了！基本属性：${this.getAttr(this.params.value)}`;
                break;

            case OperationType.End:
                if (this.params.oa && this.params.ob) {
                    this.params.desc = `战斗结束，${names[this.params.me]}${this.params.victory ? '获胜了, 士气上升' : '失败了, 士气下降'}${this.params.victory ? Math.min(50, 100 * (this.params.a / this.params.oa) / 2).toFixed(0) : Math.min(50, 100 * (this.params.b / this.params.ob) / 2).toFixed(0)}%！`;
                } else {
                    this.params.desc = `战斗结束，${names[this.params.me]}${this.params.victory ? '获胜了, 士气上升' : '失败了, 士气下降'}！`;
                }
                break;

            case OperationType.Skill:
                switch (this.params.state) {
                    case SkillStateEnum.Ready:
                        this.params.desc = `${this.src(names)}准备施放法术[${SkillArray[this.params.type].name}]`;
                        break;
                    case SkillStateEnum.Start:
                        let simStr = "";
                        if (!!this.params.sim) {
        
                            simStr = `目标：${this.params.sim.reduce((sofar, cur) => { sofar += `${this.getName(names, cur)}`; return sofar }, '')}`;
                        }
                        if (!!this.params.value) {
                            simStr += `学习了[${SkillArray[this.params.value].name}]`;
                        }
                        this.params.desc = `${this.src(names)}开始施放法术[${SkillArray[this.params.type].name}], ${simStr}`;
                        break;
                    case SkillStateEnum.Cancel:
                        this.params.desc = `${this.src(names)}的施法被打断了`;
                        break;
                    case SkillStateEnum.End:
                        this.params.desc = `${this.src(names)}结束了本次施法`;
                        break;
                }
                break;

            case OperationType.AttrChanged:
                if (!!this.params.ori) {
                    this.params.desc = `${this.src(names)}受${this.getName(names, this.params.ori)}${AttrChangedType.TypeStr(this.params.type)}的影响，属性变为：${this.getAttr(this.params.value)}`;
                } else {
                    this.params.desc = `${this.src(names)}受${AttrChangedType.TypeStr(this.params.type)}的影响，属性变为：${this.getAttr(this.params.value)}`;
                }
                break;

            case OperationType.Dead:
                this.params.desc = `${this.src(names)}阵亡了`;
                break;

            case OperationType.Alive:
                this.params.desc = `${this.src(names)}复活了`;
                break;

            case OperationType.Notify:
                this.params.desc = `${this.src(names)}收到消息：${NotifyEnum.StatusName(this.params.type)}`;
                break;

            case OperationType.BuffChanged:
                if (this.params.count > 0) {
                    this.params.desc = `${this.src(names)}战斗状态新增: ${BattleBuffEnum.StatusName(this.params.type)} 持续 ${this.params.count} 回合`;
                }
                else if (this.params.count == -1) {
                    this.params.desc = `${this.src(names)}战斗状态新增: ${BattleBuffEnum.StatusName(this.params.type)} 永久有效`;
                }
                else {
                    this.params.desc = `${this.src(names)}战斗状态失效: ${BattleBuffEnum.StatusName(this.params.type)}`;
                }
                break;

            case OperationType.Combo:
                this.params.desc = `${this.src(names)}提升连击数到${this.params.value}！`;
                break;

            case OperationType.ComboReady:
                this.params.desc = `${this.src(names)}发动了连击！`;
                break;

            case OperationType.Disappear:
                this.params.desc = `${this.src(names)}离场了！`;
                break;

            case OperationType.Effect:
                if (this.params.global) {
                    this.params.desc = `${this.src(names)}新增全局效果: ${em_Effect_List[this.params.type].name}/${this.params.value.toFixed(2)}`;
                }
                else {
                    this.params.desc = `${this.src(names)}新增效果: ${em_Effect_List[this.params.type].name}/${this.params.value.toFixed(2)}`;
                }
                break;
        }
        return this;
    }

    src(names) {
        return this.getName(names, this.params.sInfo);
    }
    /**
     * 解析
     * @param {*} params 
     */
    getName(names, params) {
        return `${names[params[0]]}的英雄${HeroConfig[parseInt(params[1])].name}(${this.getLocationDesc(params[2])})`;
    }
    getLocationDesc(loc) {
        switch (loc) {
            case 0: return '前排左';
            case 1: return '前排中';
            case 2: return '前排右';
            case 3: return '中排左';
            case 4: return '中排中';
            case 5: return '中排右';
            case 6: return '后排左';
            case 7: return '后排中';
            case 8: return '后排右';
        }
    }
    /**
     * 英雄基本属性
     * @param {*} params 
     */
    getAttr(params) {
        let ret = `攻击${params.a}, 生命${params.d}/${params.dm}, 士气${params.h}`;
        let f = params.d / params.dm;
        if (f < 0.1) {
            ret += ', 已经奄奄一息了.';
        } else if (f < 0.33) {
            ret += ', 已经摇摇欲坠了.';
        } else if (f < 0.7) {
            ret += ', 已经筋疲力尽了.';
        } else {
            ret += ', 斗志昂扬!';
        }
        return ret;
    }
}
